                                                                     
/*
 摇杆模块
 https://sourl.cn/mT8Gfd
*/
int X = 0;
int Y = 0;

int X_i = 4;
int Y_i = 1;
int B_i = 18;
int Button = 0;

void setup() {
  Serial.begin(115200);
  // pinMode(X_i, INPUT);  //定义遥感按钮的PIN为数字口A0
  // pinMode(Y_i, INPUT);  //定义遥感按钮的PIN为数字口A0
   pinMode(B_i, INPUT); 
  Serial.write("yaogan");
}

void loop() {
  X = analogRead(X_i); //遥感的X轴引脚接模拟值A2
  Y = analogRead(Y_i); //遥感的Y轴引脚接模拟值A1
  //取 X /10 的值，防止X轴和Y轴的数值过大，影响显示效果
  X = X / 10;
  Y = Y / 10;
  // 0-218  218-409

  //X设置舵机180 度的对应的比例角度


 

  Serial.write("     X_d:");
  Serial.print(jiaodu(X));
  
  Button = digitalRead(B_i);  //读取按钮的状态，并在下方打印出来
  Serial.write(" X:");
  Serial.print(X);

  Serial.write("     Y_d:");
  Serial.print(jiaodu(Y));
  Serial.write("     Y:");
  Serial.print(Y);
  Serial.write("     B:");
  Serial.println(Button);
  delay(10);

}

int jiaodu(int val){
 if (val >= 217) {
    return  map(val, 217, 409, 90, 180);
  } else{
    return map(val, 0, 217, 0, 90);
  }
}

